/*
 * StylesFrame.java
 * 
 * Created by demory on Feb 6, 2010, 9:03:12 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.gui.control;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import org.fpdev.apps.cart.CartEvent;
import org.fpdev.apps.cart.CartEventProcessor;
import org.fpdev.apps.cart.Editor;
import org.fpdev.apps.cart.gui.CartGUI;
import org.fpdev.apps.cart.gui.StylePreviewPanel;
import org.fpdev.apps.cart.network.LineStyle;
import org.fpdev.util.gui.GUIFactory;

/**
 *
 * @author demory
 */
public class StylesFrame extends ControlFrame implements ActionListener {

  private JPanel listPanel_;
  private JButton newBtn_, delBtn_, editBtn_, copyBtn_;

  private List<StyleRow> styleRows_;

  private StyleRow selectedRow_;

  public StylesFrame(CartEventProcessor cep, CartGUI gui, ControlFrameManager cfm) {
    super(cep, gui, cfm, "Styles");

    styleRows_ = new LinkedList<StyleRow>();

    listPanel_ = GUIFactory.newColumnPanel();
    JPanel listPanelWrapper = new JPanel(new BorderLayout());
    listPanelWrapper.add(listPanel_, BorderLayout.NORTH);
    JScrollPane scrollPane = new JScrollPane(listPanelWrapper);
    scrollPane.setBorder(new CompoundBorder(new EmptyBorder(0, 5, 5, 5), new BevelBorder(BevelBorder.LOWERED)));

    newBtn_ = GUIFactory.newButton("N", 30, this);
    delBtn_ = GUIFactory.newButton("D", 30, this);
    editBtn_ = GUIFactory.newButton("E", 30, this);
    copyBtn_ = GUIFactory.newButton("C", 30, this);
    JPanel buttonRow = GUIFactory.newRowPanel();
    //buttonRow.add(Box.createHorizontalGlue());
    buttonRow.add(newBtn_);
    buttonRow.add(delBtn_);
    buttonRow.add(editBtn_);
    buttonRow.add(copyBtn_);
    buttonRow.add(Box.createHorizontalGlue());

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(scrollPane, BorderLayout.CENTER);
    mainPanel.add(buttonRow, BorderLayout.NORTH);

    mainPanel.setBorder(new CompoundBorder(new LineBorder(Color.gray), new EmptyBorder(3, 0, 3, 0)));

    setLayout(new BorderLayout());
    add(mainPanel, BorderLayout.CENTER);

    pack();
    setSize(200, 200);
  }

  public void actionPerformed(ActionEvent e) {
    Editor ed = gui_.getActiveEditorFrame().getEditor();
    if(e.getSource() == newBtn_) {
      cep_.handleEvent(new CartEvent(CartEvent.Type.CREATE_STYLE, ed));
    }
    if(selectedRow_ == null) return;
    if(e.getSource() == delBtn_) {
      if(selectedRow_ == null) return;
      CartEvent event = new CartEvent(CartEvent.Type.DELETE_STYLE, ed);
      event.setProperty("style", selectedRow_.style_);
      cep_.handleEvent(event);
      selectedRow_ = null;
    }
    if(e.getSource() == editBtn_) editSelectedStyle();
    if(e.getSource() == copyBtn_) {
      CartEvent event = new CartEvent(CartEvent.Type.COPY_STYLE, ed);
      event.setProperty("style", selectedRow_.style_);
      cep_.handleEvent(event);
    }
  }

  private void editSelectedStyle() {
    Editor ed = gui_.getActiveEditorFrame().getEditor();
    CartEvent event = new CartEvent(CartEvent.Type.EDIT_STYLE, ed);
    event.setProperty("style", selectedRow_.style_);
    cep_.handleEvent(event);
  }

  public void refreshList() {
    listPanel_.removeAll();

    Collections.sort(styleRows_);
    for(StyleRow row : styleRows_) {
      row.updateLabel();
      listPanel_.add(row);
      if(row == selectedRow_)
        row.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new CompoundBorder(new LineBorder(Color.red, 2), new EmptyBorder(2, 2, 2, 2))));
      else
        row.setBorder(new EmptyBorder(3, 3, 3, 3));
      //listPanel_.add(Box.createVerticalStrut(4));
    }
    listPanel_.revalidate();
  }

  public StyleRow addStyle(LineStyle style) {
    StyleRow row = new StyleRow(style);
    styleRows_.add(row);
    System.out.println("adding style, count = "+styleRows_.size());
    refreshList();
    return row;
  }

  public void removeStyle(LineStyle style) {
    for(StyleRow row : styleRows_) {
      if(row.style_ == style) {
        styleRows_.remove(row);
        break;
      }
    }
    refreshList();
  }

  public void clear() {
    styleRows_ = new LinkedList<StyleRow>();
    selectedRow_ = null;
    refreshList();
  }

  public void rowSelected(StyleRow row) {
    selectedRow_ = row;
    refreshList();
  }

  @Override
  public void editorChanged(Editor oldEd, Editor newEd) {
    clear();
    if(newEd == null) return;

    for(LineStyle style : newEd.getDocument().getStyles().getList())
      styleRows_.add(new StyleRow(style));
   
    refreshList();
  }

  public class StyleRow extends JPanel implements Comparable<StyleRow> {

    private LineStyle style_;

    private StylePreviewPanel swatch_;
    private JLabel label_;

    public StyleRow(LineStyle style) {
      super(new BorderLayout());

      style_ = style;
      swatch_ = new StylePreviewPanel(style, 30, 18, Color.gray, 1, true);

      JPanel leftPanel = GUIFactory.newRowPanel();
      leftPanel.add(swatch_);
      leftPanel.add(Box.createHorizontalStrut(4));

      // contruct row
      add(leftPanel, BorderLayout.WEST);
      label_ = new JLabel(style_.getStyleName());
      add(label_, BorderLayout.CENTER);

      // add listeners

      addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          rowSelected(StyleRow.this);
          if(e.getClickCount() == 2) editSelectedStyle();
        }

      });
    }

    public void updateLabel() {
      label_.setText(style_.getStyleName());
    }

    public int compareTo(StyleRow o) {
      return style_.compareTo(o.style_);
    }

  }

}
